package window.frame;

import entity.HotelInfo;
import manage.HotelDBManager;
import manage.OrderDBManager;
import manage.UsernameController;
import window.tab.HotelTab;

import javax.swing.*;
import java.awt.*;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

public class HotelDetailFrame extends JFrame {
    private HotelInfo hotel;
    private HotelTab hotelTab;

    public HotelDetailFrame(HotelInfo hotel, HotelTab hotelTab) {
        this.hotelTab = hotelTab;
        this.hotel = hotel;
        setTitle("Hotel Details");
        setSize(600, 500);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setLocationRelativeTo(null);

        JPanel detailPanel = new JPanel();
        detailPanel.setLayout(new BorderLayout());
        detailPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));

        JPanel imagePanel = createImagePanel();
        detailPanel.add(imagePanel, BorderLayout.NORTH);

        JPanel infoPanel = createInfoPanel();
        detailPanel.add(infoPanel, BorderLayout.CENTER);

        add(detailPanel);
    }

    private JPanel createImagePanel() {
        JPanel imagePanel = new JPanel();
        imagePanel.setLayout(new FlowLayout(FlowLayout.LEFT));

        ImageIcon hotelImage = new ImageIcon(hotel.getImgUrl());
        Image originalImage = hotelImage.getImage();
        Image scaledImage = originalImage.getScaledInstance(300, 200, Image.SCALE_SMOOTH);
        hotelImage = new ImageIcon(scaledImage);
        JLabel imageLabel = new JLabel(hotelImage);

        imagePanel.add(imageLabel);

        return imagePanel;
    }

    private JPanel createInfoPanel() {
        JPanel infoPanel = new JPanel();
        infoPanel.setLayout(new BoxLayout(infoPanel, BoxLayout.Y_AXIS));
        infoPanel.setBorder(BorderFactory.createEmptyBorder(10, 0, 10, 10));

        JTextArea nameLabel = new JTextArea("Hotel Name: " + hotel.getName());
        nameLabel.setFont(new Font("Arial", Font.BOLD, 24));
        nameLabel.setEditable(false);
        infoPanel.add(nameLabel);

        JTextArea ratingLabel = new JTextArea("Rating: " + hotel.getRating() + "/5 stars");
        ratingLabel.setFont(new Font("Arial", Font.PLAIN, 18));
        ratingLabel.setEditable(false);
        ratingLabel.setForeground(Color.BLUE);
        infoPanel.add(ratingLabel);

        JTextArea locationLabel = new JTextArea("Location: " + hotel.getAddress());
        locationLabel.setFont(new Font("Arial", Font.PLAIN, 16));
        locationLabel.setLineWrap(true);
        locationLabel.setWrapStyleWord(true);
        locationLabel.setOpaque(false);
        locationLabel.setEditable(false);
        locationLabel.setRows(2);
        infoPanel.add(locationLabel);

        JTextArea descriptionArea = new JTextArea("Description: " + hotel.getDescription());
        descriptionArea.setFont(new Font("Arial", Font.PLAIN, 16));
        descriptionArea.setLineWrap(true);
        descriptionArea.setWrapStyleWord(true);
        descriptionArea.setOpaque(false);
        descriptionArea.setEditable(false);
        descriptionArea.setRows(3);
        infoPanel.add(descriptionArea);

        JPanel priceAndBookingPanel = creatPriceAndBookingPanel();
        infoPanel.add(priceAndBookingPanel);

        return infoPanel;
    }

    private JPanel createPriceAndRoomPanel() {
        JPanel priceAndRoomPanel = new JPanel();
        priceAndRoomPanel.setLayout(new BoxLayout(priceAndRoomPanel, BoxLayout.Y_AXIS));
        priceAndRoomPanel.setBorder(BorderFactory.createEmptyBorder(0, 5, 0, 10));

        JLabel priceLabel = new JLabel("Price: RMB " + hotel.getPrice());
        priceLabel.setFont(new Font("Arial", Font.BOLD, 18));
        priceLabel.setForeground(Color.RED);
        priceAndRoomPanel.add(priceLabel);

        JLabel roomLabel = new JLabel("Rooms Available: " + hotel.getRoom());
        roomLabel.setFont(new Font("Arial", Font.PLAIN, 16));
        priceAndRoomPanel.add(roomLabel);

        return priceAndRoomPanel;
    }

    private JPanel createBookingPanel() {
        JPanel bookingPanel = new JPanel();
        bookingPanel.setLayout(new BoxLayout(bookingPanel, BoxLayout.Y_AXIS));
        bookingPanel.setBorder(BorderFactory.createEmptyBorder(0, 10, 0, 0));
        SpinnerNumberModel roomSpinnerModel = new SpinnerNumberModel(1, 1, hotel.getRoom(), 1);
        JSpinner roomSpinner = new JSpinner(roomSpinnerModel);
        bookingPanel.add(roomSpinner);

        JButton bookButton = new JButton("Confirm");
        bookingPanel.add(bookButton);
        bookButton.setAlignmentX(Component.CENTER_ALIGNMENT);

        bookButton.addActionListener(event -> {
            if(UsernameController.getUsername() == null){
                int result = JOptionPane.showConfirmDialog(this, "Please login first!", "Confirmation", JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE);
                dispose();
            }else {
                int roomsToBook = (int) roomSpinner.getValue();
                HotelDBManager.updateHotelTable(hotel, -roomsToBook);
                System.out.println("Booking " + roomsToBook + " room(s) at hotel: " + hotel.getName());
                hotelTab.loadHotels();
                int result = JOptionPane.showConfirmDialog(this, "Booking successful!", "Confirmation", JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE);
                if (result == JOptionPane.OK_OPTION) {
                    dispose();
                }
                LocalDate today = LocalDate.now();
                DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
                String formattedDate = today.format(formatter);
                OrderDBManager.addOrder(UsernameController.getUsername(), formattedDate, "HOTEL", hotel.getName(), roomsToBook);
            }
        });

        return bookingPanel;
    }

    private JPanel creatPriceAndBookingPanel() {
        JPanel priceAndRoomPanel = createPriceAndRoomPanel();
        JPanel bookingPanel = createBookingPanel();
        JPanel priceAndBookingPanel = new JPanel();
        priceAndBookingPanel.setLayout(new BorderLayout());
        priceAndBookingPanel.add(priceAndRoomPanel, BorderLayout.WEST);
        priceAndBookingPanel.add(bookingPanel, BorderLayout.EAST);

        return priceAndBookingPanel;

    }
}
